/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package components;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author DYNG09
 */
public class ConnectServer{
    
    private Socket socket;
    
    public ConnectServer(Socket socket){
        this.socket = socket;
    }
    
    public boolean connect(){
        JLabel lIP = new JLabel("IP server");
        JTextField tfIP = new JTextField("localhost");
        JLabel lPort = new JLabel("Port");
        JTextField tfPort = new JTextField("1234");
        Object[] obj = {lIP, tfIP, lPort, tfPort};
        
        int r = JOptionPane.showConfirmDialog(null, obj, "Client - Connect to server", JOptionPane.OK_CANCEL_OPTION);
        if (r == JOptionPane.OK_OPTION) {
            try {
                setSocket(new Socket(tfIP.getText(), Integer.parseInt(tfPort.getText())));
                return true;
            } catch (UnknownHostException ex) {
                JOptionPane.showMessageDialog(null, "Unknown destination host");
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Some IO error occurred");
            }
        }
        
        return false;
    }

    /**
     * @return the socket
     */
    public Socket getSocket() {
        return socket;
    }

    /**
     * @param socket the socket to set
     */
    public void setSocket(Socket socket) {
        this.socket = socket;
    }
}
